//
//  DropMenuView.m
//  小团购
//
//  Created by 樊荣海 on 15/1/23.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "DropMenuView.h"

@implementation DropMenuView {
    
    CoverView *_cover;
    
    CateView *_cateView;
    
    UIView *_contentView;//连接scrollView+cateView

}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        
        //自动伸缩
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        //1.蒙板
        _cover = [CoverView coverWithTarget:self action:@selector(tapClick:)];
        _cover.frame = self.bounds;
        
//        _cover.backgroundColor = [UIColor blackColor];
//        _cover.alpha = 0.5;
//        _cover.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//        [_cover addGestureRecognizer:tap];
        
        
        [self addSubview:_cover];
        
        //2.contenView
        _contentView = [[UIView alloc] init];
        _contentView.frame = CGRectMake(0, 0, self.frame.size.width, DropScrollViewH);
        _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self addSubview:_contentView];
        
        //3.scrollView
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.frame = CGRectMake(0, 0, self.frame.size.width, DropScrollViewH);
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage resizedImage:@"separator_filter_item"]];
        [_contentView addSubview:scrollView];
        _scrollView = scrollView;
        
        
    }
    
    return self;
}

- (void)tapClick:(UITapGestureRecognizer *)sender {
    
    if (_block) {
        _block();
    }
    
}

//显示 动画
- (void)showAnimation {
    
    _contentView.transform = CGAffineTransformMakeTranslation(0, -_contentView.frame.size.height);
    _contentView.alpha = 0;
    _cover.alpha = 0;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _contentView.transform = CGAffineTransformIdentity;
        _contentView.alpha = 1;
        [_cover resetAlpha];
        
    } completion:^(BOOL finished) {
        
    }];
    
    
}

//隐藏 动画
- (void)hideAniamtion {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _contentView.transform = CGAffineTransformMakeTranslation(0, -_contentView.frame.size.height);
        _cover.alpha = 0;
        _contentView.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        _contentView.transform = CGAffineTransformIdentity;
        _contentView.alpha = 1;
        _cover.alpha = 0.5;
    }];
}

//[重要]父类的点击方法
- (void)btnClick:(DropMenuBtn *)sender {
    
    _currentBtn.selected = NO;
    sender.selected = YES;
    _currentBtn = sender;
    
    if (sender.titles.count) {
        // 显示所有的子标题
        [self showSubtitlesView:sender];
    } else {
        // 隐藏所有的子标题
        [self hideSubtitlesView:sender];
        
        
    }
    
}

#warning cateViewBUG
- (void)showSubtitlesView:(DropMenuBtn *)sender {
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:0.5];
    
    //显示所有的子标题
    if (_cateView == nil) {
        _cateView = [[CateView alloc] init];
        _cateView.frame = CGRectMake(0, DropScrollViewH, self.frame.size.width, _cateView.frame.size.height);//高度暂时不确定
        
        __unsafe_unretained DropMenuView *selfWeak = self;
        _cateView.block = ^(NSString *subTitle){
            [[DBTool sharedDBTool] insertSubTitle:subTitle btn:sender];
            
            [selfWeak tapClick:nil];
            
        };
    }
    
    _cateView.titleArray = sender.titles;
    
    if ([_cateView superview] == nil) {//没有被 addSubView -- 执行动画
        [_cateView showAnimation];
    }
    
    [_contentView insertSubview:_cateView belowSubview:_scrollView];
    
    
    //改变contentView的frame
    CGRect rect = _contentView.frame;
    rect.size.height = DropScrollViewH + _cateView.frame.size.height;
    _contentView.frame = rect;
    
//    [UIView commitAnimations];
    
}

- (void)hideSubtitlesView:(DropMenuBtn *)sender {
    //移除分类标题
    [_cateView hideAniamtion];
    
    //再次改变contentView的frame
    CGRect rect = _contentView.frame;
    rect.size.height = DropScrollViewH;
    _contentView.frame = rect;
    
    [[DBTool sharedDBTool] insertSubTitle:[_currentBtn titleForState:UIControlStateNormal] btn:_currentBtn];
    
    if (_block) {
        _block();
    }
}






@end
